<?php
namespace app\serverarticle\logic;

use think\Db;
use \app\manage\logic\BaseLogic;

/**
 * 文章微应用下的逻辑层
 * @author  蒋哲 2017.5.23
 */
class Article extends BaseLogic
{
    protected $name = 'article';
    protected $config = 'db_config_article';
    protected $classifyModel = 'app\serverarticle\model\Article';

    // 文章发布/添加
    public function articleAdd($data)
    {
        $rst = $this->model($this->classifyModel)->save($data);
        $rstId = $this->model($this->classifyModel)->id;

        if ($rst === false) {
            return auto_failure ($data,'文章添加失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章未作改动！');
        }
        return auto_return(['id' => $rstId],'添加成功！');
    }

    //文章编辑
    public function articleEdit($data)
    {
        $data['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where('id',$data['id'])
                    ->update($data);
        if ($rst === false) {
            return auto_failure ($data,'分类信息更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类信息未作改动！');
        }

        return auto_return(['number' => $rst],'分类信息更改成功！');
    }

    // 置顶某篇文章
    public function top($data): array
    {
        $updateData['top'] = 1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章置顶成功！');
    }

    // 取消置顶某篇文章
    public function cancelTop($data): array
    {
        $updateData['top'] = 0;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已取消置顶！');
    }

    // 推荐某篇文章
    public function recommended($data): array
    {
        $updateData['recommended'] = 1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已推荐！');
    }

    // 取消推荐某篇文章
    public function cancelRecommended($data): array
    {
        $updateData['recommended'] = 0;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已取消推荐！');
    }

    // 删除某篇文章
    public function delete($data): array
    {
        $updateData['status'] = 0;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已删除！');
    }

    // 回复启用某篇文章
    public function enable($data): array
    {
        $updateData['status'] = 1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已回复启用！');
    }

    // 上架/重新发布某篇文章
    public function release($data): array
    {
        $updateData['release_status'] = 1;
        $updateData['release_time'] = time();
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已重新上架！');
    }

    // 下架某篇文章
    public function cancelRelease($data): array
    {
        $updateData['release_status'] = 0;
        $updateData['under_time'] = time();
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章已下架！');
    }

    // 阅读权限 仅自己可见
    public function oneselfVisible($data): array
    {
        $updateData['arcrank'] = 0;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    // 阅读权限 所有人可见
    public function everyoneVisible($data): array
    {
        $updateData['arcrank'] = 1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    // 阅读权限 指定人可见
    public function nominatorVisible($data): array
    {
        $updateData['arcrank'] = 2;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    // 阅读权限 指定人不可见
    public function nominatorInvisible($data): array
    {
        $updateData['arcrank'] = 3;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    // 评论权限 允许评论
    public function review($data): array
    {
        $updateData['review_status'] = 1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    // 评论权限 禁止评论
    public function banReview($data): array
    {
        $updateData['review_status'] = 0;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    // 转载权限 允许转载
    public function reprinted($data): array
    {
        $updateData['reprinted_status'] = 1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    // 转载权限 禁止转载
    public function banReprinted($data): array
    {
        $updateData['reprinted_status'] = 0;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    // 审核通过
    public function audit($data): array
    {
        $updateData['audit_status'] = 1;
        $updateData['audit_id'] = $data['audit_id'];
        $updateData['audit_time'] = time();
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where('id',$data['id'])
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    // 审核失败
    public function auditFailure($data): array
    {
        $updateData['audit_status'] = -1;
        $updateData['audit_id'] = $data['audit_id'];
        $updateData['audit_time'] = time();
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where('id',$data['id'])
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章状态已修改！');
    }

    // 指定文章可见人
    public function visible($data): array
    {
        $updateData['visible'] = json_encode($data["visible"]);
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where('id',$data['id'])
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '指定可见人数量已修改！');
    }

    // 指定文章不可见人
    public function invisible($data): array
    {
        $updateData['invisible'] = json_encode($data["invisible"]);
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where('id',$data['id'])
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '指定不可见人数量已修改！');
    }

    // 点赞 次数+1
    public function praise($data): array
    {
        $praiseNum = $this->db()->where($data)->value('praise');
        $updateData['praise'] = $praiseNum + 1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章点赞次数已更新！');
    }

    // 踩 次数+1
    public function trample($data): array
    {
        $trampleNum = $this->db()->where($data)->value('trample');
        $updateData['trample'] = $trampleNum + 1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章被踩次数已更新！');
    }

    // 点击/浏览 次数+1
    public function click($data): array
    {
        $clickNum = $this->db()->where($data)->value('click');
        $updateData['click'] = $clickNum + 1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章点击/浏览次数已更新！');
    }

    // 评论条数 次数+1
    public function comments($data): array
    {
        $commentsNum = $this->db()->where($data)->value('comments');
        $updateData['comments'] = $commentsNum + 1;
        $updateData['lastpost_time'] = time();
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章评论条数已更新！');
    }

    // 转载次数 次数+1
    public function reprint($data): array
    {
        $reprintNum = $this->db()->where($data)->value('reprint');
        $updateData['reprint'] = $reprintNum + 1;
        $updateData['update_time'] = time();
        $rst = $this->model($this->classifyModel)
                    ->where($data)
                    ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'文章状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章状态未发生修改！');
        }
        return auto_return(['number' => $rst], '文章转载次数已更新！');
    }

    // 全部文章获取
    public function articleList($data)
    {
        $manageAppKey = \app\manage\service\ApiBase::getAppKey();
        $_whereOr = $manageAppKey == '4942b00d6a75eebcce692224b0cb3110' ? true : 'article_classify.`key` = "' . $manageAppKey . '"';
        $_where['article.status'] = ['neq', 0];
        $data['classify_id'] && $_where['article.classify_id'] = $data['classify_id'];
        $data['keywords'] && $_where['article.id|article.title|article.author'] = ['like', '%' . $data['keywords'] . '%'];
        $list = Db::connect(config('db_config_article'))
                ->view('article','*')
                ->view('article_classify','name','article_classify.id = article.classify_id')
                ->where($_where)
                ->where($_whereOr)
                ->order('update_time', 'desc')
                ->paginate($data['listRows'], false, ['page'=>$data['page']])
                ->toArray();
        for ($i=0; $i < count($list["data"]); $i++) {
            $list["data"][$i]["create_time"] = date("Y-m-d",$list["data"][$i]["create_time"]);
            $list["data"][$i]["update_time"] = date("Y-m-d",$list["data"][$i]["update_time"]);
            if ($list["data"][$i]["status"] == 0) {
                $list["data"][$i]["status_text"] = "删除";
            } else {
                $list["data"][$i]["status_text"] = "正常";
            }
            if ($list["data"][$i]["audit_status"] == -1) {
                $list["data"][$i]["audit_text"] = "审核失败";
            } elseif ($list["data"][$i]["audit_status"] == 0) {
                $list["data"][$i]["audit_text"] = "未审核";
            } else {
                $list["data"][$i]["audit_text"] = "审核通过";
            }
        }
        return auto_return_page($list);

    }

    // 前台 获取某一类文章列表 非删除 非置顶 非推荐 已发布 所有人可见 审核通过
    public function someList($data): array
    {
        $_where['classify_id']    = $data['classify_id'];
        $_where['status']         = ['eq',1];
        $_where['top']            = ['eq',0];
        $_where['recommended']    = ['eq',0];
        $_where['release_status'] = ['eq',1];
        $_where['arcrank']        = ['eq',1];
        $_where['audit_status']   = ['eq',1];

        $list = $this->db()
                     ->where($_where)
                     ->order('update_time', 'desc')
                     ->paginate($data['listRows'], false, ['page'=>$data['page']])
                     ->toArray();
        return auto_return_page($list);
    }

    // 获取某一篇文章信息 非删除
    public function articleInfo($data): array
    {
        $_where = array(
            "article.id" => $data['id'],
            "article.status" => ['eq',1],
        );

        $res = Db::connect(config('db_config_article'))
                ->view('article','*')
                ->view('article_classify','name','article_classify.id = article.classify_id')
                ->where($_where)
                ->find();
        if (!empty($res)) {
            $res['cate_ids'] = logic('\app\serverarticle\logic\ArticleClassify')->getParentIds($res['classify_id']);
            return auto_return($res);
        }
        return auto_failure($res,"查询文章不存在！");
    }

    // 前台 获取某一类置顶文章列表 非删除 非推荐 已发布 所有人可见 审核通过
    public function topList($data): array
    {
        $_where['classify_id']    = $data['classify_id'];
        $_where['status']         = ['eq',1];
        $_where['top']            = ['eq',1];
        $_where['recommended']    = ['eq',0];
        $_where['release_status'] = ['eq',1];
        $_where['arcrank']        = ['eq',1];
        $_where['audit_status']   = ['eq',1];

        $list = $this->db()
                     ->where($_where)
                     ->order('update_time', 'desc')
                     ->paginate($data['listRows'], false, ['page'=>$data['page']])
                     ->toArray();
        return auto_return_page($list);
    }

    // 前台 获取某一类推荐状态文章列表 非删除 非置顶 已发布 所有人可见 审核通过
    public function recommendedList($data): array
    {
        $_where['classify_id']    = $data['classify_id'];
        $_where['status']         = ['eq',1];
        $_where['top']            = ['eq',0];
        $_where['recommended']    = ['eq',1];
        $_where['release_status'] = ['eq',1];
        $_where['arcrank']        = ['eq',1];
        $_where['audit_status']   = ['eq',1];

        $list = $this->db()
                     ->where($_where)
                     ->order('update_time', 'desc')
                     ->paginate($data['listRows'], false, ['page'=>$data['page']])
                     ->toArray();
        return auto_return_page($list);
    }

    // 前台 获取某一类所有文章列表 非删除 已发布 所有人可见 审核通过
    public function allList($data): array
    {
        $_where['classify_id']    = $data['classify_id'];
        $_where['status']         = ['eq',1];
        $_where['release_status'] = ['eq',1];
        $_where['arcrank']        = ['eq',1];
        $_where['audit_status']   = ['eq',1];

        $list = $this->db()
            ->where($_where)
            ->order('update_time', 'desc')
            ->paginate($data['listRows'], false, ['page'=>$data['page']])
            ->toArray();
        return auto_return_page($list);
    }
}
